<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="openDear('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round>
            添加面经
          </el-button>
        </div>
      </template>
      <el-table
        :data="list"
        style="width: 100%">
        <el-table-column
          label="标题"
          prop="stem"
          width="400">
        </el-table-column>
          <el-table-column
          label="作者"
          prop="creator"
          width="140">
        </el-table-column>
          <el-table-column
          label="点赞"
          prop="likeCount"
          width="140">
        </el-table-column>
          <el-table-column
          label="浏览数"
          prop="views"
          width="140">
        </el-table-column>
          <el-table-column
          label="更新时间"
          prop="createdAt"
          width="200">
        </el-table-column>
          <el-table-column
          label="操作">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDear('preview',row.id)"></i>
              <i class="el-icon-edit-outline" @click="openDear('deit',row.id)"></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="q.current"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="q.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!-- ination
        @size-change="handleSizeChange"   每页显示的条数改变触发
        @current-change="handleCurrentChange"  当前页码值改变时触发
        :current-page="currentPage4"  当前的页码值
        :page-sizes="[100, 200, 300, 400]"  每页显示条数的选项
        :page-size="100"              控制每页显示的条数
        layout="total, sizes, prev, pager, next, jumper"   用来给分页器布局
        :total="400">         总的条数
      </el-pagination> -->
    </el-card>
      <el-drawer
      size=50%
      :title="derTitle"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="handleClose">
      <div v-if="drawerType === 'preview'" class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>
      <el-form v-else ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content" @blur="$refs.form.validateField('content')"></quill-editor>
        </el-form-item>
         <el-form-item>
            <el-button type="primary" @click="subimt">确定</el-button>
            <el-button>取消</el-button>
          </el-form-item>
      </el-form>
    </el-drawer>

  </div>
</template>

<script>
import { addArtivles, getArtivles, removeArticle, getaddArticle, delArtivles } from '@/api/article'
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      q: {
        current: 1,
        pageSize: 10
      },
      list: [],
      total: 0,
      drawer: false,
      drawerType: 'add'
    }
  },
  computed: {
    derTitle () {
      let title = '添加面经'
      if (this.drawerType === 'add') title = '添加面经'
      if (this.drawerType === 'preview') title = '预览面经'
      if (this.drawerType === 'deit') title = '修改面经'
      return title
    }
  },
  created () {
    this.loadgetArtivles()
  },
  methods: {
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeArticle(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadgetArtivles()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async loadgetArtivles () {
      const res = await getArtivles(this.q)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.q.current = 1
      this.q.pageSize = val
      this.loadgetArtivles()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.q.current = val
      this.loadgetArtivles()
    },
    async openDear (type, id) {
      // console.log(type)
      console.log(id)
      this.drawer = true
      this.drawerType = type
      // 如果不是添加
      if (type !== 'add') {
        // 发送axios
        const res = await getaddArticle(id)
        console.log(res)
        // 获取
        this.form = res.data
      }
    },
    handleClose (done) {
      if (this.drawerType !== 'preview') {
        this.$refs.form.resetFields()
      }

      this.form = {
        stem: '',
        content: ''
      }
      this.drawer = false
    },
    async subimt () {
      try {
        // 兜底
        await this.$refs.form.validate()
        // 变量
        let res = null
        // 判断 是哪个
        if (this.drawerType === 'add') {
          res = await addArtivles(this.form)
        } else {
          // 解构
          const { id, stem, content } = this.form
          res = await delArtivles({ id, stem, content })
        }
        // 发送axios

        // 判断正确
        if (res.code !== 10000) return this.$message.error(res.message)

        this.$message.success('添加成功')
        this.q.current = 1
        // 渲染
        this.loadgetArtivles()
        // 关闭
        this.handleClose()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
